const { Toast } = require('antd-mobile');
const { Actions } = require("react-native-router-flux");
import { default as constant, ERROR_MSG_DURATION } from './constant';
const { Linking } = require("react-native");
export const distanceStr = (distance) => {
    const d = distance > 1000 ? distance / 1000 : distance;
    const unit = distance > 1000 ? 'Km' : 'm';
    return `${Math.ceil(d * 100) / 100}${unit}`;
};
export const tookQty = (qty) => {
    if (qty == 0) {
        return 30;
    }
    if (qty <= 5 && qty < 10) {
        return Math.ceil(qty * 10 + parseInt(qty));
    }
    if (qty < 100) {
        return Math.ceil(qty * 1.5);
    }
    return qty;
};
export const chooseRate = (rate) => {
    if (rate > 100) {
        return 99;
    }
    return rate;
};
export const checkToken = (auth, fail = null) => {
    if (auth instanceof Object && Object.keys(auth).length > 0) {
        if (auth.isSignIn) {
            if (auth.profile instanceof Object && Object.keys(auth.profile).length > 0) {
                if (auth.profile.token && auth.profile.token.length > 0) {
                    return true;
                }
            }
        }
    }
    fail && fail();
    return false;
};
export const checkResponse = (data, code = [402, 403], msg = '账户信息已过期，或者在其他地方登陆过,请重新登录') => {
    let success = true;
    if (code instanceof Array) {
        for (let i = 0; i < code.length; i++) {
            if (code[i] == data.code) {
                success = false;
                break;
            }
        }
    }
    else if (code == data.code) {
        success = false;
    }
    if (!success) {
        if (msg) {
            Toast.fail(msg, 1.5);
        }
    }
    return success;
};
export const checkSns = (auth, channel = '') => {
    let info = null;
    if (auth.profile.sns && auth.profile.sns.length > 0) {
        auth.profile.sns.map((sns) => {
            if (sns.platform == 'web' && sns.channel == channel) {
                info = sns;
            }
        });
    }
    if (auth.sns && auth.sns.length > 0) {
        auth.profile.sns.map((sns) => {
            if (sns.platform == 'web' && sns.channel == channel) {
                info = sns;
            }
        });
    }
    return info;
};
export function array_chunk(input, size, preserve_keys) {
    let x, p = '', i = 0, c = -1, l = input.length || 0, n = [];
    if (size < 1) {
        return null;
    }
    if (Object.prototype.toString.call(input) === '[object Array]') {
        if (preserve_keys) {
            while (i < l) {
                (x = i % size) ? n[c][i] = input[i] : n[++c] = {}, n[c][i] = input[i];
                i++;
            }
        }
        else {
            while (i < l) {
                (x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
                i++;
            }
        }
    }
    else {
        if (preserve_keys) {
            for (p in input) {
                if (input.hasOwnProperty(p)) {
                    (x = i % size) ? n[c][p] = input[p] : n[++c] = {}, n[c][p] = input[p];
                    i++;
                }
            }
        }
        else {
            for (p in input) {
                if (input.hasOwnProperty(p)) {
                    (x = i % size) ? n[c][x] = input[p] : n[++c] = [input[p]];
                    i++;
                }
            }
        }
    }
    return n;
}
export const toastValidateError = (errors, message = '') => {
    errors && Object.keys(errors).map((errorKey) => {
        errors[errorKey].errors.map((e, i) => {
            message += e.message + (errors[errorKey].errors.length - 1 !== i ? '' : "\n");
        });
    });
    Toast.fail(message, ERROR_MSG_DURATION);
};
export const toastValidateSuccess = (errors, message = '') => {
    errors && Object.keys(errors).map((errorKey) => {
        errors[errorKey].errors.map((e, i) => {
            message += e.message + (errors[errorKey].errors.length - 1 !== i ? '' : "\n");
        });
    });
    Toast.success(message, ERROR_MSG_DURATION);
};
export const getValueFromArray = (array, field, value) => {
    let _value = null;
    if (!field) {
        return _value;
    }
    if (!value) {
        return _value;
    }
    _value = [];
    array.map(item => {
        if (typeof item === 'string') {
            if (item === value) {
                _value.push(item[field]);
            }
        }
        else if (typeof item === 'object') {
            if (item[field] === value) {
                _value.push(item[field]);
            }
        }
    });
    if (_value.length === 0) {
        return null;
    }
    else if (_value.length === 1) {
        return _value[0];
    }
    else {
        return _value;
    }
};
export const openUrl = (source, inApp = false) => {
    if (inApp) {
        Actions.web({ source });
    }
    else {
        Linking.canOpenURL(source).then(supported => {
            if (!supported) {
                Toast.fail('无法打开链接:' + source, constant.ToastDuration);
            }
            else {
                return Linking.openURL(source);
            }
        }).catch(err => {
            Toast.error(err.message, constant.ToastDuration);
        });
    }
};
export const checkRole = (roles) => {
    if (!roles || roles.length <= 0)
        return false;
    const roleStr = roles.map((r) => r.code).join('|');
    if (roleStr.indexOf('market') !== -1) {
        return false;
    }
    if (roleStr.indexOf('normal') !== -1) {
        return false;
    }
    return true;
};
